package com.company.nuwa.common.annotation;

import com.company.nuwa.common.utils.ValidatorUtils;
import jakarta.validation.Constraint;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.Payload;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * <p>生日校验器</p >
 *
 * @author Nikola Tesla
 * @version 1.0
 * @date 2020/11/15 14:19
 */
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {Birthday.BirthdayValidator.class})
@Documented
public @interface Birthday {

    String message() default "utils.validation.Birthday.message";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class BirthdayValidator implements ConstraintValidator<Birthday, String> {

        @Override
        public void initialize(Birthday constraintAnnotation) {
        }

        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return ValidatorUtils.isBirthDay(value);
        }

    }

}

